package edu.yctc.weblog.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import edu.yctc.weblog.constant.ResultCode;
import edu.yctc.weblog.dao.BlogTagDAO;
import edu.yctc.weblog.dao.TagDAO;
import edu.yctc.weblog.dto.TagDTO;
import edu.yctc.weblog.entity.PageDO;
import edu.yctc.weblog.entity.ResultDO;
import edu.yctc.weblog.entity.TagDO;
import edu.yctc.weblog.enums.TagDeleteStatus;
import edu.yctc.weblog.service.TagService;
import edu.yctc.weblog.utils.PageQueryUtil;

@Service("tagService")
public class TagServiceImpl implements TagService {

    private final static Logger LOG = LoggerFactory.getLogger("serviceLogger");

    @Resource
    private TagDAO tagDAO;

    @Resource
    private BlogTagDAO blogTagDAO;

    @Override
    public ResultDO<PageDO> getBlogTagPage(PageQueryUtil pageUtil) {
        List<TagDO> tagDOList = tagDAO.getTagList(pageUtil);
        int total = tagDAO.getTotalTags();
        PageDO pageDO = new PageDO(total, pageUtil.getLimit(), pageUtil.getPage(), tagDOList);
        LOG.info("get blog tag page success, tags={}", tagDOList);
        return new ResultDO<PageDO>(true, ResultCode.SUCCESS, ResultCode.MSG_SUCCESS, pageDO);
    }

    @Override
    public ResultDO<Integer> getTotalTags() {
        return new ResultDO<>(true, ResultCode.SUCCESS, ResultCode.MSG_SUCCESS, tagDAO.getTotalTags());
    }

    @Override
    public ResultDO<Void> saveTag(String tagName) {
        if (StringUtils.isBlank(tagName)) {
            LOG.error("save tag fail, parameter invalid, name={}", tagName);
            return new ResultDO<>(false, ResultCode.PARAMETER_INVALID, ResultCode.MSG_PARAMETER_INVALID);
        }
        TagDO tagDO = new TagDO();
        tagDO.setName(tagName);
        tagDO.setIsDeleted(TagDeleteStatus.UNDELETED);
        tagDAO.insert(tagDO);
        LOG.info("save tag success, name={}", tagName);
        return new ResultDO<>(true, ResultCode.SUCCESS, ResultCode.MSG_SUCCESS);
    }

    @Override
    public ResultDO<Void> deleteBatch(Long[] ids) {
        if (ids.length < 1) {
            LOG.error("delete tag fail, no category to delete");
            return new ResultDO<>(false, ResultCode.PARAMETER_INVALID, ResultCode.MSG_PARAMETER_INVALID);
        }
        for (Long id : ids) {
            if (id <= 0) {
                LOG.error("delete tag fail, parameter invalid, id={}", id);
                return new ResultDO<>(false, ResultCode.PARAMETER_INVALID, ResultCode.MSG_PARAMETER_INVALID);
            }
        }
        List<Long> blogTags = blogTagDAO.getDistinctTagIds(ids);
        // 存在关联关系不删除
        if (blogTags.size() > 0) {
            LOG.error("delete tag fail, foreign key limit, blog tag relation list={}", blogTags);
            return new ResultDO<>(false, ResultCode.TAG_HAS_RELATION_WITH_BLOG,
                ResultCode.MSG_TAG_HAS_RELATION_WITH_BLOG);
        }
        tagDAO.deleteBatch(ids);
        LOG.info("delete tags success");
        return new ResultDO<>(true, ResultCode.SUCCESS, ResultCode.MSG_SUCCESS);
    }

    @Override
    public ResultDO<List<TagDTO>> getBlogTagCountForIndex() {
        return new ResultDO<>(true, ResultCode.SUCCESS, ResultCode.MSG_SUCCESS, tagDAO.getTagCount());
    }

}
